<?php
class auspostContract
{
    var $dateTime;
    var $activity;
    var $facilityLocation;
}
class Uuch_AuspostAPI {
    function Query($trackId)
    { 
        $ch = curl_init("http://auspost.com.au/track/track.html");
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "trackIds=".$trackId); 
        $html =curl_exec($ch);
        curl_close($ch);
        $seletpreg = '%<table.*?>(.*?)</table>%si';
        preg_match_all($seletpreg, $html, $match);
        $result =$this->getArray($match[0][0]);
        unset($result[0]);
        $resultArray=array();
        foreach ($result as $value) {
            $auspost=new auspostContract();
            $auspost->dateTime=$value[0];
            $auspost->activity=$value[1];
            $auspost->facilityLocation=$value[2];
            array_push($resultArray, $auspost);
        }
        return $resultArray;
    }
    
      private function getArray($table) {
        $table = preg_replace("'<table[^>]*?>'si", "", $table);
        $table = preg_replace("'<tr[^>]*?>'si", "", $table);
        $table = preg_replace("'<td[^>]*?>'si", "", $table);
        $table = str_replace("</tr>", "{tr}", $table);
        $table = str_replace("</td>", "{td}", $table);
        //去掉 HTML 标记
        $table = preg_replace("'<[\/\!]*?[^<>]*?>'si", "", $table);
        //去掉空白字符 
        $table = preg_replace("'([\r\n])[\s]+'", "", $table);
        $table = explode('{tr}', $table);
        array_pop($table);
        foreach ($table as $key => $tr) {
            $td = explode('{td}', $tr);
            array_pop($td);
            $td_array[] = $td;
        }
        return $td_array;
    }
}

?>
